# General purpose plotting functions
# 1. ploti (plot by interactions): 
ploti <- function(exppar, natp=512, nptp=64, ps=F, yrange=c(0,1)){
# arguments:
#  exppar: object name of experiment results, a list
#  natp  : number of total possible test points
#  nptp  : size of test points population
#  ps    : whether or not output to a ps file
#
    # Set data object
    #expnames <-  c("at.as","co.as","co.ws","co.ai","co.wi")
    #nexp <- length(expnames)
    
    out0301 <- exppar$at.as
    out0401 <- exppar$co.as
    out0402 <- exppar$co.ws
    out0403 <- exppar$co.mo
    out0405 <- exppar$co.ai
    out0406 <- exppar$co.wi
    
    # Indexing
    ratio = natp / nptp
    ng0301 <- min(dim(out0401)[1]%/%ratio,dim(out0301)[1])
    xrange <- c(1,ng0301)
    print(paste(ratio,ng0301))
    idx2 <- seq(ratio,dim(out0401)[1],by=ratio)

    # Set output
    if(ps){
        ofile <- paste(deparse(substitute(exppar)),".ps",sep="")
        print(paste("Output to",ofile))
        postscript(ofile)
    }

    # Plotting paramters
    #par(mfrow=c(3,2))
    par(mfrow=c(4,2))

    # 0301
    plot(out0301[1:ng0301,1],xlim=xrange,ylim=yrange,type="l",
         xlab=paste("Interaction (*",natp,")"),ylab="Best Fitness")
    for(i in 2:10){
        lines(out0301[1:ng0301,i],type="l",lty=i)
    }
    title("all-test-points / average-score")

    # 0401
    plot(out0401[idx2,1],xlim=xrange,ylim=yrange,type="l",
         xlab=paste("Interaction (*",natp,")"),ylab="Best Fitness")
    for(i in 2:10){
        lines(out0401[idx2,i],type="l",lty=i)
    }
    title("coevolution / average-score")

    # 0402
    plot(out0402[idx2,1],xlim=xrange,ylim=yrange,type="l",
         xlab=paste("Interaction (*",natp,")"),ylab="Best Fitness")
    for(i in 2:10){
        lines(out0402[idx2,i],type="l",lty=i)
    }
    title("coevolution / weighted-score")

    # 0403
    plot(out0403[idx2,1],xlim=xrange,ylim=yrange,type="l",
         xlab=paste("Interaction (*",natp,")"),ylab="Best Fitness")
    for(i in 2:10){
        lines(out0403[idx2,i],type="l",lty=i)
    }
    title("coevolution / MOO")

    # 0405
    plot(out0405[idx2,1],xlim=xrange,ylim=yrange,type="l",
         xlab=paste("Interaction (*",natp,")"),ylab="Best Fitness")
    for(i in 2:10){
        lines(out0405[idx2,i],type="l",lty=i)
    }
    title("coevolution / average-info")

    # 0406
    plot(out0406[idx2,1],ylim=yrange,type="l",
         xlab=paste("Interaction (*",natp,")"),ylab="Best Fitness")
    for(i in 2:10){
        lines(out0406[idx2,i],type="l",lty=i)
    }
    title("coevolution / weighted-infoe")

    # Average
    a0301 <- apply(out0301,1,mean)
    a0401 <- apply(out0401,1,mean)
    a0402 <- apply(out0402,1,mean)
    a0403 <- apply(out0403,1,mean)
    a0405 <- apply(out0405,1,mean)
    a0406 <- apply(out0406,1,mean)
    # Main trend
    plot(a0301[1:ng0301],xlim=xrange,ylim=yrange,type="l",lty=1,col=1,
         xlab=paste("Interaction (*",natp,")"),ylab="Best Fitness")
    lines(a0401[idx2],lty=2,col=2)
    lines(a0402[idx2],lty=3,col=3)
    lines(a0403[idx2],lty=4,col=4)
    lines(a0405[idx2],lty=5,col=5)
    lines(a0406[idx2],lty=6,col=6)
    # Legend
    lposx <- ng0301/2
    lposy <- 0.3*(yrange[2]-yrange[1]) + yrange[1]
    #print(c(lposx,lposy))
    legend(lposx,lposy,ncol=2, c("GAAS","COAS","COWS","COMO","COAI","COWI"),
           lty=c(1,2,3,4,5,6), col=c(1,2,3,4,5,6), cex=0.7)
    #legend(lposx,lposy,ncol=2, c("GAAS","COAS","COWS","COAI","COWI"),
    #       lty=c(1,2,3,5,6), col=c(1,2,3,5,6), cex=0.7)
    title("Averaged Best Fitness")
    
    plot.new()
    # Closing device and clean up
    if(ps)
        dev.off()
}

# General purpose plotting functions
# 1. plotco (plot by interactions): 
plotco <- function(exppar, natp=512, nptp=64, ps=F, yrange=c(0,1)){
# arguments:
#  exppar: object name of experiment results, a list
#  natp  : number of total possible test points
#  nptp  : size of test points population
#  ps    : whether or not output to a ps file
#
    # Set data object
    #expnames <-  c("at.as","co.as","co.ws","co.ai","co.wi")
    #nexp <- length(expnames)
    
    out0401 <- exppar$co.as
    out0402 <- exppar$co.ws
    out0403 <- exppar$co.mo
    out0405 <- exppar$co.ai
    out0406 <- exppar$co.wi
    
    # Indexing
    #ratio = natp/nptp
    ratio <- 1
    ng0301 <- dim(out0401)[1]%/%ratio
    xrange <- c(1,ng0301)
    print(paste(ratio,ng0301))
    idx2 <- seq(ratio,dim(out0401)[1],by=ratio)

    # Set output
    if(ps){
        ofile <- paste(deparse(substitute(exppar)),".ps",sep="")
        print(paste("Output to",ofile))
        postscript(ofile)
    }

    # Plotting paramters
    par(mfrow=c(3,2))

    # 0401
    plot(out0401[idx2,1],xlim=xrange,ylim=yrange,type="l",
         xlab=paste("Interaction (*",natp,")"),ylab="Best Fitness")
    for(i in 2:10){
        lines(out0401[idx2,i],type="l",lty=i)
    }
    title("coevolution / average-score")

    # 0402
    plot(out0402[idx2,1],xlim=xrange,ylim=yrange,type="l",
         xlab=paste("Interaction (*",natp,")"),ylab="Best Fitness")
    for(i in 2:10){
        lines(out0402[idx2,i],type="l",lty=i)
    }
    title("coevolution / weighted-score")

    # 0403
    plot(out0403[idx2,1],xlim=xrange,ylim=yrange,type="l",
         xlab=paste("Interaction (*",natp,")"),ylab="Best Fitness")
    for(i in 2:10){
        lines(out0403[idx2,i],type="l",lty=i)
    }
    title("coevolution / MOO")

    # 0405
    plot(out0405[idx2,1],xlim=xrange,ylim=yrange,type="l",
         xlab=paste("Interaction (*",natp,")"),ylab="Best Fitness")
    for(i in 2:10){
        lines(out0405[idx2,i],type="l",lty=i)
    }
    title("coevolution / average-info")

    # 0406
    plot(out0406[idx2,1],xlim=xrange,ylim=yrange,type="l",
         xlab=paste("Interaction (*",natp,")"),ylab="Best Fitness")
    for(i in 2:10){
        lines(out0406[idx2,i],type="l",lty=i)
    }
    title("coevolution / weighted-infoe")

    # Average
    #a0301 <- apply(out0301,1,mean)
    a0401 <- apply(out0401,1,mean)
    a0402 <- apply(out0402,1,mean)
    a0403 <- apply(out0403,1,mean)
    a0405 <- apply(out0405,1,mean)
    a0406 <- apply(out0406,1,mean)
    # Main trend
    plot(a0401[idx2],type="l",lty=2,col=2,xlim=xrange,ylim=yrange,
         xlab=paste("Generation"),ylab="OFC")
    lines(a0402[idx2],type="l",lty=3,col=3)
    lines(a0403[idx2],type="l",lty=4,col=4)
    lines(a0405[idx2],type="l",lty=5,col=5)
    lines(a0406[idx2],type="l",lty=6,col=6)
    # Legend
    lposx <- ng0301/2
    legend(lposx,0.8,ncol=2, c("GAAS","COAS","COWS","COMO","COAI","COWI"),
           lty=c(1,2,3,4,5,6), col=c(1,2,3,4,5,6), cex=0.7)
    #legend(lposx,0.2,ncol=2, c("GAAS","COAS","COWS","COAI","COWI"),
    #       lty=c(1,2,3,5,6), col=c(1,2,3,5,6), cex=0.7)
    title("Averaged OFC")
        
    #plot.new()
    # Closing device and clean up
    if(ps)
        dev.off()
}

